package com.bluesky.drcp.business.service;

import java.util.Iterator;
import java.util.List;

import com.bluesky.drcp.business.Role;
import com.bluesky.drcp.business.UserRole;
import com.bluesky.drcp.persistence.dao.UserRoleDao;

public class UserRoleService {
	private UserRoleDao userRoleDao;

	public UserRoleDao getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	public boolean isAdmin(String userId) {
		List roleList = userRoleDao.getRoleList(userId);
		for (Iterator it = roleList.iterator(); it.hasNext();) {
			Role role = (Role) it.next();
			if (role.getName().equals("admin"))
				return true;
		}
		return false;
	}

	public List getRoleList(String userId) {
		return userRoleDao.getRoleList(userId);
	}

	public void setRoleList(String userId, List roleIdList) {
		userRoleDao.delete(userId);

		Iterator it = roleIdList.iterator();
		while (it.hasNext()) {
			String roleId = (String) it.next();
			UserRole ur = new UserRole();
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			userRoleDao.save(ur);
		}
	}

}
